package com.tianqu.tms.www.assess.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.www.assess.actionform.AssessSerchInputForm;
import com.tianqu.tms.www.assess.controller.CarAssessInputResultController;
import com.tianqu.tms.www.assess.viewhelper.AssessCarInfoInputViewHelper;

/**
 * 
 * 进入到车辆评估信息录入打印页面
 * 
 * @author Administrator
 * 
 */
public class CarAssessInputResultAction extends TmsAction {
	private Logger logger = Logger.getLogger(CarAssessInputResultAction.class);

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");

		AssessSerchInputForm inputform = (AssessSerchInputForm) form;
		String assessPirce = inputform.getAssessPirce();

		AssessCarInfoInputViewHelper view = (AssessCarInfoInputViewHelper) uc
				.getAttribute(TmsConstants.VIEWHELP);

		if (view.getLastPgPrice() != null && !"".equals(view.getLastPgPrice())) {
			String lastTime = view.getLastPgTime();
			String outTime = DateUtil.getAfterMonth1(1, lastTime);
			outTime = outTime.replace("-", "");
			String currentTime = DateUtil.getCurrentTimeStr();
			currentTime = currentTime.replace("-", "");

			if (Integer.parseInt(outTime) >= Integer.parseInt(currentTime)) {
				if (Integer
						.parseInt(StringUtil.removeDH(view.getLastPgPrice())) < Integer
						.parseInt(StringUtil.removeDH(assessPirce))) {
					setErrorsToRequest(request, TmsConstants.IM004, null);
					return mapping.getInputForward();
				}
			} else {
				if (Integer
						.parseInt(StringUtil.removeDH(view.getLastPgPrice())) <= Integer
						.parseInt(StringUtil.removeDH(assessPirce))) {
					setErrorsToRequest(request, TmsConstants.IM004, null);
					return mapping.getInputForward();
				}
			}
		}

		isToken(mapping, request);

		CarAssessInputResultController controller = new CarAssessInputResultController();
		controller.execute(uc);
		int marketType = view.getMarketType();
		String actionForward = "";
		String backBsflag = (String) uc.getAttribute("backBsflag");
		uc.removeAttribute("backBsflag");

		if (backBsflag.equals("1")) {
			actionForward = "updateTop";
		}
		if (backBsflag.equals("2")) {
			actionForward = "inputTop";
		}

		view.setPrintBsflag("1");

		view.setCarName(view.getVendorCode() + " " + view.getModelCode());

		Codes code = Codes.getInstance();

		view.setCarTypedy(code.getItemValueByItemIdAndItemCode(
				Codes.USER_NATURE, Integer.parseInt(view.getCarUse())));

		view.setAssessPircedy(view.getAssessPirce());
		
		view.setSysdate(DateUtil.getCurrentTimeStr2());

		uc.removeAttribute(TmsConstants.VIEWHELP);
		request.setAttribute(TmsConstants.VIEWHELP, view);
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(actionForward);
	}

}
